﻿using System;
using System.Text;
using System.Collections.Generic;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Chaliy.DbPro.Generators.TextAuthoring;
using Chaliy.DbPro.Generators.Tests.TextAuthoring.Mocks;

namespace Chaliy.DbPro.Generators.Tests.TextAuthoring
{   
    [TestClass]
    public class RandomSentanceAuthorTests
    {
        [TestMethod]
        [DeploymentItem(DeploymentConstants.GeneratorsDll)]
        public void TestNormalFlowGenerationSeparators()
        {			
			ITermAccessor separatorTerms = TermAccessorUtilities.CreateWithDefaultSeparators();
			ITermAccessor comaTerms = TermAccessorUtilities.CreateWithDefaultComas();
			IWordAuthor wordAuthor = WordAuthorUtilities.CreateWithThreeItems();

			string result = RandomSentanceAuthorTests.ExecuteGenerate(
				separatorTerms, comaTerms, wordAuthor
				);

			TermAccessorUtilities.AssertTextContainsSomething(result, separatorTerms);
        }

		//    // Unfortunately this test can fail. This is because this is no way to give garanties
		//    // that coma will be generated with in sentance
		//[TestMethod]
		//[DeploymentItem(DeploymentConstants.GeneratorsDll)]
		//public void TestNormalFlowGenerationComas()
		//{		

		//    ITermAccessor separatorTerms = TermAccessorUtlities.CreateWithDefaultSeparators();
		//    ITermAccessor comaTerms = TermAccessorUtlities.CreateWithDefaultComas();
		//    IWordAuthor wordAuthor = WordAuthorUtilities.CreateWithThreeItems();

		//    string result = RandomSentanceAuthorTests.ExecuteGenerate(
		//        separatorTerms, comaTerms, wordAuthor
		//        );

		//    TermAccessorUtlities.AssertContinsSomething(result, comaTerms);
		//}

		[TestMethod]
		[DeploymentItem(DeploymentConstants.GeneratorsDll)]
		public void TestNormalFlowGenerationWordSamples()
		{			
			ITermAccessor separatorTerms = TermAccessorUtilities.CreateWithDefaultSeparators();
			ITermAccessor comaTerms = TermAccessorUtilities.CreateWithDefaultComas();
			ITermAccessor wordSampleTerms = TermAccessorUtilities.CreateWithThreeWordSamples();
			IWordAuthor wordAuthor = new RandomWordAuthor(wordSampleTerms);

			string result = RandomSentanceAuthorTests.ExecuteGenerate(
				separatorTerms, comaTerms, wordAuthor
				);

			TermAccessorUtilities.AssertTextContainsSomething(result, wordSampleTerms);
		}

		[TestMethod]
		[DeploymentItem(DeploymentConstants.GeneratorsDll)]
		public void TestGeneratorWithZeroMinAndMaxLength()
		{
			ITermAccessor separatorTerms = TermAccessorUtilities.CreateWithDefaultSeparators();
			ITermAccessor comaTerms = TermAccessorUtilities.CreateWithDefaultComas();
			ITermAccessor wordSampleTerms = TermAccessorUtilities.CreateWithThreeWordSamples();
			IWordAuthor wordAuthor = new RandomWordAuthor(wordSampleTerms);

			ISentanceAuthor author = new RandomSentanceAuthor(separatorTerms, comaTerms, wordAuthor) 
			{
				MinLength = 0,
				MaxLength = 0 
			};
			string result = author.Generate();

			TermAccessorUtilities.AssertTextContainsSomething(result, separatorTerms);
		}

		[TestMethod]
		[DeploymentItem(DeploymentConstants.GeneratorsDll)]
		[ExpectedException(typeof(TextAuthorException))]
		public void TestGeneratorWithEmptySeprators()
		{
			ITermAccessor separatorTerms = TermAccessorUtilities.CreateWithEmpty();
			ITermAccessor comaTerms = TermAccessorUtilities.CreateWithEmpty();
			ITermAccessor wordSampleTerms = TermAccessorUtilities.CreateWithEmpty();
			IWordAuthor wordAuthor = new RandomWordAuthor(wordSampleTerms);

			ISentanceAuthor author = new RandomSentanceAuthor(separatorTerms, comaTerms, wordAuthor)
			{
				MinLength = 0,
				MaxLength = 0
			};
			
			// Expected exception because author cannot work with empty seprators
			author.Generate();			
		}

		[TestMethod]
		[DeploymentItem(DeploymentConstants.GeneratorsDll)]
		[ExpectedException(typeof(ArgumentNullException))]
		public void TestConstructorWithEmptyArgumentSeparators()
		{
			// Expecitn exception here because 
			// author cannot work without separators term accessor
			new RandomSentanceAuthor(
				null,
				TermAccessorUtilities.CreateWithEmpty(),
				new MockWordAuthor("Rob")
				);
		}

		[TestMethod]
		[DeploymentItem(DeploymentConstants.GeneratorsDll)]
		[ExpectedException(typeof(ArgumentNullException))]
		public void TestConstructorWithEmptyArgumentComas()
		{
			// Expecitn exception here because 
			// author cannot work without comas term accessor
			new RandomSentanceAuthor(
				TermAccessorUtilities.CreateWithEmpty(),
				null,
				new MockWordAuthor("Rob")
				);
		}

		[TestMethod]
		[DeploymentItem(DeploymentConstants.GeneratorsDll)]
		[ExpectedException(typeof(ArgumentNullException))]
		public void TestConstructorWithEmptyArgumentWordAuthor()
		{
			// Expecitn exception here because 
			// author cannot work without word author
			new RandomSentanceAuthor(
				TermAccessorUtilities.CreateWithEmpty(),
				TermAccessorUtilities.CreateWithEmpty(),
				null
				);
		}

		private static string ExecuteGenerate(ITermAccessor separatorTerms, ITermAccessor comaTerms, IWordAuthor wordAuthor)
		{
			ISentanceAuthor author = new RandomSentanceAuthor(separatorTerms, comaTerms, wordAuthor) 
			{
				MinLength = 10,
				MaxLength = 12 
			};
			return author.Generate();			
		}
				
    }
}
